<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthAdmin
{
    public function handle(Request $request, Closure $next)
    {
        if (Auth::guard('admin')->check()) {
            $user_id = Auth::guard('admin')->id();
            //超级用户 (   id = 1 用户 )直接通过

            if ($user_id === 1) {
                return $next($request);
            }
            return $next($request);
//            //获取当前访问路由信息
//            $route_uri = Route::current()->uri();
//            dump($route_uri);
//            //获取当前 角色
//            $user_role = User::find($user_id)->role;
//            $own_permissions = [];          //所有角色权限
//            foreach ($user_role as $value) {
//                $role = Role::find($value->id);
//                $own_perms = $role->permission;                //当前角色权限
//                foreach ($own_perms as $val) {
//                    $own_permissions[] = $val->url;
//                }
//            }
//            //去重数组
//            $own_permissions = array_unique($own_permissions);
//            if (in_array($route_uri, $own_permissions)) {
//                return $next($request);     //通过验证
//            } else {
//                return redirect('no_access',302);       //无权访问
//            }
        } else {
            return redirect()->route('manager_login');       //未登录
        }
    }
}
